package logic

import (
	"api/internal/svc"
	"api/internal/types"
	"api/service"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}
type WeChatLoginResponse struct {
	OpenID     string `json:"openid"`
	SessionKey string `json:"session_key"`
	UnionID    string `json:"unionid"`
	ErrCode    int    `json:"errcode"`
	ErrMsg     string `json:"errmsg"`
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.RequestDeviceLogin) (resp *types.ResponseDeviceLogin, err error) {
	// 用户永久登录
	// 判断手机号是否存在
	// 存在则返回信息
	// 不存在则
	redis := l.svcCtx.RedisPool.Get()
	defer redis.Close()
	busService := service.NewBusinessService(l.ctx, l.svcCtx)
	token, err := busService.BusLogin(redis, req.Phone, req.Password)
	if err == nil {
		return &types.ResponseDeviceLogin{
			Token: token,
			Msg:   "返回token成功",
			Code:  200,
		}, nil
	} else {
		return &types.ResponseDeviceLogin{
			Token: "",
			Msg:   err.Error(),
			Code:  200,
		}, nil
	}
}
